import {get,set } from 'lodash';

export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        context.suspendPaint();
        try {
            const { table, store, col: currentCol, sheet, sheetName,oldDelete ,deleteColCount} = options;
            const bindingPath = table.bindingPath();
            if (isUndo) {
                const {
                     tableRow, tableCol, rowCount, colCount, currentCol,deleteColCount,_oldWidth
                } = options.old;
                sheet.addColumns(currentCol,deleteColCount);
                sheet.tables.resize(table,rowCount, colCount);
                sheet.setColumnWidth(currentCol, _oldWidth);
                options._oldSpans.forEach(range => {
                    sheet.addSpan(range.row, range.col, range.rowCount, range.colCount);
                });
                //store.dataSourceInstance =options._oldDataSource;
                options._oldDataSource.forEach((data) => {
                    store.dataSourceInstance.setValue(data.key, data.value);
                })
                
                // 回滚值
                for (let r= 0; r < rowCount; r++) {
                    const row = tableRow + r;
                    //for (let c = 0; c < colCount; c++) {
                        const col = currentCol;
                        let cell;
                        cell = sheet.getCell(row, col);
                        console.log(options._oldValue[row][col]);
                        cell.cellType(options._oldCellTypes[row][col])
                            .backColor(options._oldBackColor[row][col])
                            .textIndent(options._oldTextIndent[row][col])
                            .backgroundImageLayout(options._oldBackgroundImageLayout[row][col])
                            .backgroundImage(options._oldBackGroundImage[row][col])
                            .bindingPath(options._oldBindingPath[row][col])
                            .font(options._oldFont[row][col])
                            .hAlign(options._oldHAlign[row][col])
                            .vAlign(options._oldVAlign[row][col])
                            .value(options._oldValue[row][col]);
                    //}
                }
            } else {
                const records = options.oldValue = store.dataSourceInstance.getValue(bindingPath);
                const { row: tableRow , col : tableCol, rowCount, colCount} = table.dataRange();
                const source = options._oldDataSource = store.dataSourceInstance.getSource()
                options._oldSpans = [];
                // 合并单元格
                for(let r = 0; r < rowCount ; r++) {
                    const cr = r + tableRow;
                    for(let c = 0 ; c < colCount; c++) {
                        const cc = c +tableCol;
                        const span = sheet.getSpan(cr, cc);
                        if(span && span.row == cr && span.col == cc) {
                            options._oldSpans.push(span);
                        }
                    }
                }

                options.old = {
                     tableRow, tableCol, rowCount, colCount, currentCol,deleteColCount, _oldWidth:sheet.getColumnWidth(currentCol)
                    
                };
                options._oldBackColor = {};
                options._oldCellTypes = {};
                options._oldBackGroundImage = {};
                options._oldBackgroundImageLayout = {};
                options._oldTextIndent = {};
                options._oldValue = {};
                options._oldBindingPath = {};
                options._oldFont = {};
                options._oldHAlign = {};
                options._oldVAlign = {};
                options._oldSpans =[]
                options._oldDataSource = [];
                const data = [];
                let existCt = '@';
                for (let r = 0; r < rowCount; r++) {
                    const row = r + tableRow;
                    if (!options._oldBackColor[row]) {
                        options._oldBackColor[row] = {}
                    }
                    if (!options._oldCellTypes[row]) {
                        options._oldCellTypes[row] = {};
                    }
                    if (!options._oldTextIndent[row]) {
                        options._oldTextIndent[row] = {};
                    }
                    if (!options._oldBackGroundImage[row]) {
                        options._oldBackGroundImage[row] = {};
                    }
                    if (!options._oldHAlign[row]) {
                        options._oldHAlign[row] = {};
                    }
                    if (!options._oldVAlign[row]) {
                        options._oldVAlign[row] = {};
                    }
                    if (!options._oldBackgroundImageLayout[row]) {
                        options._oldBackgroundImageLayout[row] = {};
                    }
                    if (!options._oldFont[row]) {
                        options._oldFont[row] = {};
                    }
                    if (!options._oldValue[row]) {
                        options._oldValue[row] = {};
                    }
                    if (!options._oldBindingPath[row]) {
                        options._oldBindingPath[row] = {};
                    }
                   // for (let c = 0; c < colCount; c++) {
                        const col = currentCol;
                        const cell = sheet.getCell(row, col);
                        options._oldBackColor[row][col] = cell.backColor();
                        options._oldCellTypes[row][col] = cell.cellType();
                        options._oldTextIndent[row][col] = cell.textIndent();
                        options._oldBackGroundImage[row][col] = cell.backgroundImage();
                        options._oldBackgroundImageLayout[row][col] = cell.backgroundImageLayout();
                        options._oldFont[row][col] = cell.font();
                        options._oldValue[row][col] = cell.value();
                        options._oldVAlign[row][col] = cell.vAlign();
                        options._oldHAlign[row][col] = cell.hAlign();
                        const bingdingPath = options._oldBindingPath[row][col] = cell.bindingPath();

                        if(bingdingPath) {
                            const cellType = cell.cellType();
                            const strs = bingdingPath.split(".");
                            if(existCt.indexOf("@" +strs[1] +'|'+strs[2] + "@") <= -1) {
                                const path = strs[0] + "." + strs[1] + "." + strs[2];
                                options._oldDataSource.push({'key':path,'value': store.dataSourceInstance.getValue(path)});
                                existCt = existCt + strs[1] +'|'+strs[2] + "@" ;
                                //data.push(source[strs[0]][strs[1]][strs[2]]);
                                console.log("塞入",strs[1] +'|'+strs[2],existCt);
                                data.push(strs)
                            } else {
                                console.log("存在")
                            }
                                                        
                        }

                   // }
                }
                for(let i = 0 ; i < data.length ; i++){
                    const strs = data[i];
                    console.log(strs,"strs")
                    delete source[strs[0]][strs[1]][strs[2]]
                };

                // 删除逻辑
                sheet.deleteColumns(currentCol,deleteColCount);
                sheet.tables.resize(table,rowCount, colCount - deleteColCount);

            }
            table.bindingPath(bindingPath);
            store.calcAll(true, sheetName);
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        context.resumePaint();
    }
};